import Taro from "@tarojs/taro";
// 是否是 json 字符串
export function isJsonString(str) {
    try {
        if (typeof JSON.parse(str) == "object") {
            return true;
        }
    }
    catch (e) {
        return false;
    }
    return false;
}
//缓存数据
export var setCache = function (key, value) {
    var params = value;
    if (typeof value == "object") {
        params = JSON.stringify(value);
    }
    return Taro.setStorageSync(key, params);
};
//读取缓存
export function getCache(key) {
    var result = Taro.getStorageSync(key);
    if (result) {
        if (isJsonString(result)) {
            result = JSON.parse(result);
        }
    }
    else {
        // 返回{} 避免解构时 运行时错误
        return {};
    }
    return result;
}
//删除缓存
export var removeCache = function (key) {
    return Taro.removeStorageSync(key);
};
export default {
    setCache: setCache,
    getCache: getCache,
    removeCache: removeCache
};
